package com.mahesh;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	String password, username;
	Connection conn;
	boolean connect = false;
	ResultSet rs = null;
	PrintWriter out = null;

	public Login() {
		super();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		out = response.getWriter();
		username = request.getParameter("name");
		password = request.getParameter("pwd");
		response.setContentType("text/html");
		HttpSession session = request.getSession();
		if (password != "" && password != "null") {
		if (username != "" && username != "null") {

			Service serv = new Service();

			if (serv.authenicate(username, password)) {

				session.setAttribute("user", username);
				response.sendRedirect("/Jsplogin/Success.jsp");
			}

			else {
				System.out.println("OOps!!! Invalid Username/Password");
				RequestDispatcher rq1 = request
						.getRequestDispatcher("/index.jsp");
				rq1.forward(request, response);
			}
		}
		} else {
			response.sendRedirect("/Jsplogin/index.jsp");

		}
	}
}